package com.nsn.vs.sdm.util.svn;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.nsn.vs.sdm.component.svn.SvnClientManager;
import com.nsn.vs.sdm.component.svn.command.SvnCommandCheckout;
import com.nsn.vs.sdm.component.svn.command.SvnCommandCommit;

/**
 * Utilities for SVN operations, e.g. check-out, check-in and etc.
 * 
 * @author Jeccy.Zhao
 *
 */
public class UtilSvn
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(UtilSvn.class);
	
	/**
	 * User name for authentication
	 */
	private String userName;
	
	/**
	 * User password for authentication
	 */
	private String password;
	
	/**
	 * Instance of SVN client manager
	 */
	private SvnClientManager manager = null;
	
	/**
	 * The sole constructor
	 * 
	 * @param userName
	 * @param password
	 */
	public UtilSvn (String userName, String password)
	{
		this.userName = userName;
		this.password = password;
		
		// initialization
		init();
	}
	
	/**
	 * Initialization
	 */
	private void init()
	{
		manager = new SvnClientManager (userName, password);
	}
	
	/**
	 * Check-out 
	 * 
	 * @param svnPath
	 * @param destPath
	 * @return
	 * @throws Exception 
	 */
	public void checkOut (String svnPath, String destPath) throws Exception
	{
		LOGGER.info("Start to checkout from repository:" + svnPath);
		manager.executeCommand(new SvnCommandCheckout(manager, svnPath, destPath));
	}
	
	/**
	 * 
	 * @param itemsBeAdded
	 * @param itemsBeRemoved
	 * @param comment
	 * @throws Exception
	 */
	public void commit (List<String> itemsBeAdded, List<String> itemsBeRemoved, 
			String comment) throws Exception
	{
		manager.executeCommand(new SvnCommandCommit(
			manager, itemsBeAdded, itemsBeRemoved, comment));
	}
	
	/**
	 * List all entries laid in the specified URL
	 * 
	 * @param root
	 * @param url
	 * @throws Exception
	 * @return
	 */
	public List<String> listEntries (String path) throws Exception
	{
		return manager.listEntries(path);
	}
	
	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public String getUserName()
	{
		return userName;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getPassword()
	{
		return password;
	}

	public void setManager(SvnClientManager manager)
	{
		this.manager = manager;
	}

	public SvnClientManager getManager()
	{
		return manager;
	}

	public static Logger getLogger()
	{
		return LOGGER;
	}
	
	public static void main (String[] args)
	{
		// logger initialization
		BasicConfigurator.configure();
		PropertyConfigurator.configure("log4j.properties"); 
		
		UtilSvn svn = new UtilSvn("cofan", "Nokia123$");
		
		String repoPath = "D:\\svn\\com.nsn.nthlrfe\\com.nsn.nthlrfe-HI50.01\\o2ml\\extra\\AutomationTools\\src\\AA";
		
		List<String> filesToAdd = new ArrayList<String>();
		filesToAdd.add(repoPath + "\\debug.txt");
		
		try {
			svn.commit(filesToAdd, null, "only for testing...");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
